import { MessageAlert } from '../../../../../lib/message_alert'
import { MessageType } from '../../../../../types/constants'
import { SYMBOLS } from '../../../../../types/interfaces'

export class AutoPaginationMessage {
  private static instance: AutoPaginationMessage
  private readonly messageAlert: MessageAlert = MessageAlert.getInstance()
  private readonly indicatorId: string

  constructor(indicatorId: string) {
    this.indicatorId = indicatorId
  }

  /**
   * 获取单例实例
   */
  public static getInstance(indicatorId: string): AutoPaginationMessage {
    if (!AutoPaginationMessage.instance) {
      AutoPaginationMessage.instance = new AutoPaginationMessage(indicatorId)
    }
    return AutoPaginationMessage.instance
  }

  /**
   * 显示加载中
   */
  showLoadingMessage(pageNum: number): void {
    this.messageAlert.showMessage({
      id: this.indicatorId,
      content: `正在加载第 ${pageNum} 页...`,
      autoHide: false,
    })
  }

  /**
   * 显示完成
   */
  showCompletionMessage(count: number): void {
    this.messageAlert.showMessage({
      id: this.indicatorId,
      content: `
        <div style="text-align: center;">
          <div style="color: #4CAF50; font-weight: bold">
            ${SYMBOLS.STATUS.SUCCESS} 成功加载了 ${count} 页内容
          </div>
        </div>
      `,
      type: MessageType.SUCCESS,
      autoHide: true,
      duration: 3000,
    })
  }

  /**
   * 显示取消
   */
  showCancelMessage(count: number): void {
    this.messageAlert.showMessage({
      id: this.indicatorId,
      content: `
        <div style="text-align: center;">
          <div style="color: #ffa502; font-weight: bold;">
            ${SYMBOLS.STATUS.WARNING} 已取消批量加载
          </div>
          <div style="font-size: 12px; color: #666;">
            已加载 ${count} 页内容
          </div>
        </div>
      `,
      type: MessageType.WARNING,
      autoHide: true,
      duration: 2000,
    })
  }

  /**
   * 显示错误
   */
  showErrorMessage(message: string): void {
    this.messageAlert.showMessage({
      id: this.indicatorId,
      content: `
        <div style="text-align: center;">
          <div style="color: #ff4757; font-weight: bold; margin-bottom: 5px;">
            ${SYMBOLS.STATUS.ERROR} ${message}
          </div>
        </div>
      `,
      type: MessageType.ERROR,
      autoHide: true,
      duration: 5000,
    })
  }

  /**
   * 显示无内容
   * @private
   */
  showNoResultMessage(): void {
    this.messageAlert.showMessage({
      content: `
        <div style="text-align: center !important;">
          <div>${SYMBOLS.STATUS.WARNING} 未获取到新内容，可以稍后重试</div>
          <div>（3分钟内禁用滚动加载，请手动加载）</div>
        </div>`,
      type: MessageType.WARNING,
    })
  }

  /**
   * 显示已加载过
   */
  showHasLoadedMessage(): void {
    this.messageAlert.showMessage({
      id: this.indicatorId,
      content: `${SYMBOLS.STATUS.WARNING} 所有分页已加载过`,
      type: MessageType.WARNING,
      autoHide: true,
    })
  }

  /**
   * 显示已加载最大页码
   */
  showMaxLoadedMessage(maxPageNum: number): void {
    this.messageAlert.showMessage({
      id: this.indicatorId,
      content: `${SYMBOLS.STATUS.WARNING} 已加载到最大页码(${maxPageNum})，跳过处理`,
      type: MessageType.WARNING,
      autoHide: true,
    })
  }
}
